package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.StringUtil;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * 更改密碼
 * Created by cumt on 2017/6/20.
 */

public class PersonChangePwdActivity  extends BaseActivity implements View.OnClickListener {

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonChangePwdActivity.class);

        activity.startActivity(intent);
    }
    private Activity activity;

    private ImageView imgBack;
    private TextView txtTitle;

    private ImageView img_input_type;

    private EditText et_old_pwd,et_new_pwd;
    private TextView btn_change_pwd_ok,txt_forget_pwd;

    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;

    private  PersonalNomalInfo loginuserinfo;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_change_pwd);
    }

    @Override
    public void initView() {

        activity= PersonChangePwdActivity.this;

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("密码变更");

        et_old_pwd = (EditText)findViewById(R.id.et_old_pwd);
        et_new_pwd= (EditText)findViewById(R.id.et_new_pwd);

        btn_change_pwd_ok= (TextView)findViewById(R.id.btn_change_pwd_ok);
        txt_forget_pwd= (TextView)findViewById(R.id.txt_forget_pwd);

        img_input_type= (ImageView) findViewById(R.id.img_input_type);


        PersonalNomalInfo user = LocalInfo.getUserInfo(PersonChangePwdActivity.this);
        params = new HashMap<>();
        params.put("mobile",user.getMobile());

        initRequest();
        initWatcher();


         loginuserinfo = LocalInfo.getLoginInfo(activity);

    }

    @Override
    public void setListener() {

    }

    private void initWatcher(){
        TextWatcher cname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                if(VilidateUtil.isPwd(String.valueOf(et_new_pwd.getText()))){
                    //登錄密碼
                        if(!StringUtil.isBlank(String.valueOf(params.get("name")))){

                            params.put("password",String.valueOf(et_new_pwd.getText()));

                            btn_change_pwd_ok.setBackgroundResource(0);
                            btn_change_pwd_ok.setBackgroundResource(R.drawable.btn_red_shape);
                        }
                        else{

                            Toast.makeText(activity, "6位以上，數字加字母",
                                    Toast.LENGTH_SHORT).show();
                        }

                }
                else{

                    Toast.makeText(activity, "6位以上，數字加字母",
                            Toast.LENGTH_SHORT).show();

                }

            }
        };

        et_new_pwd.addTextChangedListener(cname);


        TextWatcher oname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


                if(VilidateUtil.isPwd(String.valueOf(et_old_pwd.getText()))){

                    //登錄密碼
                    if( String.valueOf(et_new_pwd.getText()).equals(loginuserinfo.getPassword())){

                        params.put("name",String.valueOf(et_old_pwd.getText()));
                        if(!StringUtil.isBlank(String.valueOf(params.get("password")))){
                            btn_change_pwd_ok.setBackgroundResource(0);
                            btn_change_pwd_ok.setBackgroundResource(R.drawable.btn_red_shape);

                        }

                    }
                    else{
                        Toast.makeText(activity, "密碼不對",
                                Toast.LENGTH_SHORT).show();
                    }

                }
                else{

                    Toast.makeText(activity, "6位以上，數字加字母",
                            Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void afterTextChanged(Editable s) {



            }
        };

        et_old_pwd.addTextChangedListener(oname);



    }

    @Override
    public void doEvent() {

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        txt_forget_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PersonSendCodeActivity.gotoActivity(activity);

            }
        });

        img_input_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                boolean isread = img_input_type.getDrawable().getCurrent().getConstantState().equals(getResources().getDrawable(R.mipmap.icon_me_eyeclose3x).getCurrent().getConstantState());

                Log.e("img_input_type===",""+isread);
                if(isread){
                    img_input_type.setImageResource(R.mipmap.icon_me_eyeopen3x);

                    et_old_pwd.setInputType(InputType.TYPE_CLASS_TEXT);
                    et_new_pwd.setInputType(InputType.TYPE_CLASS_TEXT);
                }
                else{
                    img_input_type.setImageResource(R.mipmap.icon_me_eyeclose3x);
                    et_old_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    et_new_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

                }

            }
        });

        btn_change_pwd_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isread = btn_change_pwd_ok.getBackground().getCurrent().getConstantState().equals(getResources().getDrawable(R.drawable.btn_red_shape).getCurrent().getConstantState());
                if(isread){
                    httpModel.startRequest();
                }

            }
        });

    }

    private void initRequest(){
        httpModel = new HttpModel(RequestUrl.baseUrl+RequestUrl.updateUserUrl,params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                    if(VilidateUtil.isSuccess(resultCode)){

                        Toast.makeText(activity, "修改成功",
                                Toast.LENGTH_SHORT).show();
                    }
                else{
                        Toast.makeText(activity, resultMsg+",修改失敗",
                                Toast.LENGTH_SHORT).show();
                    }

            }
        },true);


    }
}
